//
//  StageBaseViewController.m
//  NewGame
//
//  Created by wwj on 15/10/22.
//  Copyright © 2015年 wwj. All rights reserved.
//

#import "CountScoreViewController.h"
#import "StageBaseViewController.h"
#import "PauseViewController.h"
#import "StageRecordModel.h"
#import "IngScoreView.h"
#import "ReadyGoView.h"
#import "StageModel.h"

@interface StageBaseViewController ()

@end

@implementation StageBaseViewController

-(void)setStageModel:(StageModel *)stageModel{
    _stageModel = stageModel;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self initView];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(observerNotification) name:@"enterForeground" object:nil];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onClickRetry) name:@"restartGame" object:nil];
    
    //显示教程
    if (!_stageModel.recordModel.rank) {
        UIImageView *imageView = [[UIImageView alloc] init];
        imageView.userInteractionEnabled = YES;
        imageView.frame = self.view.bounds;
        [self.view addSubview:imageView];
        
        NSMutableArray *imageArray = [NSMutableArray array];
        for (NSString *imageName in [self getGuideImageName]) {
            [imageArray addObject:[UIImage imageNamed:imageName]];
        }
        
        imageView.animationImages =imageArray;
        imageView.animationDuration = 0.15 * imageArray.count;
        [imageView startAnimating];
        [imageView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onClickImage:)]];
    }else{
        [self beginGame];
    }
}

-(void)initView{
    IngScoreView *ingScoreView = [IngScoreView ingScoreView];
    _ingScoreView = ingScoreView;
    [self.view addSubview:ingScoreView];
    
    ReadyGoView *readyGoView = [ReadyGoView readyGoView];
    _readyGoView = readyGoView;
    [self.view addSubview:readyGoView];
    
    UIButton *retryBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [retryBtn setImage:[UIImage imageNamed:@"ing_retry"] forState:UIControlStateNormal];
    [retryBtn addTarget:self action:@selector(onClickRetry) forControlEvents:UIControlEventTouchUpInside];
    retryBtn.frame = CGRectMake(301, 161, 150, 60);
    [self.view addSubview:retryBtn];
    
    UIButton *pauseBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [pauseBtn setImage:[UIImage imageNamed:@"ing_pause"] forState:UIControlStateNormal];
    [pauseBtn addTarget:self action:@selector(onClickPause) forControlEvents:UIControlEventTouchUpInside];
    pauseBtn.frame = CGRectMake(301, 214, 150, 71);
    [self.view addSubview:pauseBtn];
   
}

#pragma -mark 开始游戏
-(void)beginGame{
    //积分板
    [_ingScoreView begin];
    [_readyGoView beginWithBlock:nil];
}

#pragma -mark 结束游戏
-(void)endGame{
    self.view.userInteractionEnabled = NO;
    
    CountScoreViewController *countScoreVC = [CountScoreViewController countScoreViewController];
    countScoreVC.scoreNew = _score;
    countScoreVC.stageModel = self.stageModel;
    [self.navigationController pushViewController:countScoreVC animated:YES];
}

#pragma -mark 重新开始游戏
- (void)onClickRetry{
}

#pragma -mark 暂停游戏
- (void)onClickPause{
    PauseViewController *pauseVC = [PauseViewController pauseViewController];
    pauseVC.pauseBlock=nil;
    [self.navigationController pushViewController:pauseVC animated:YES];
}

-(NSArray *)getGuideImageName{
    return nil;
}

-(void)onClickImage:(UITapGestureRecognizer *)tap{
    [tap.view removeFromSuperview];
    [self beginGame];
}

-(void)observerNotification{
    [self onClickPause];
}

-(void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}



@end
